/*************************************************************************
 > File Name: test.c
 > Author: LiuTianpeng
 > Mail: ltpforever@163.com
 > Created Time: 2017年07月24日 星期一 15时22分00秒
 ************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "database.h"
#include  "ret.h"

#define TR_QUERY_NUM 5
#define ERR_QUERY_NUM 5

//#define DEBUG
db_register data_nw = { TABLE_NW, TABLE_NW, TABLE_NW };
db_register data_xj = { TABLE_XJ, TABLE_XJ, TABLE_XJ };

unsigned int inttoBCD(int m);

void debug_tr(tran_record* tr) {
	int i = 0;
	printf("-------------------debug NW  Tran Record-----------------\n");
	printf("tr.guntab = %d\n", tr->guntab);
	printf("tr.flag_finished =%02x\n", tr->flag_finished);

	printf("tr.serialnum = ");
	for (i = 0; i < 16; i++)
		printf("%02x", tr->serialnum[i]);
	printf("\n");

	printf("tr.starttime = ");
	for (i = 0; i < 7; i++)
		printf("%02x", tr->starttime[i]);
	printf("\n");

	printf("tr.endtime = ");
	for (i = 0; i < 7; i++)
		printf("%02x", tr->endtime[i]);
	printf("\n");

	printf("tr.charge_duration = %d\n", tr->charge_duration);

	printf("tr.payCard = ");
	for (i = 0; i < 8; i++)
		printf("%02x", tr->payCard[i]);
	printf("\n");

	printf("tr.userId = ");
	for (i = 0; i < 8; i++)
		printf("%02x", tr->userId[i]);
	printf("\n");

	printf("tr.cardId = ");
	for (i = 0; i < 8; i++)
		printf("%02x", tr->cardId[i]);
	printf("\n");

	printf("tr.modelID = ");
	for (i = 0; i < 8; i++)
		printf("%02x", tr->modelID[i]);
	printf("\n");

	printf("tr.stop_reason = %d\n", tr->stop_reason);
	printf("tr.billbytm = %d\n", tr->billbytm);

	for (i = 0; i < 5; i++)
		printf("w_start[%d] = %d\n", i, tr->w_start[i]);

	for (i = 0; i < 5; i++)
		printf("w_end[%d] = %d\n", i, tr->w_end[i]);

	for (i = 0; i < 5; i++)
		printf("w[%d] = %d\n", i, tr->w[i]);

	for (i = 0; i < 5; i++)
		printf("cost_elec[%d] = %d\n", i, tr->cost_elec[i]);

	printf("tr.cost_server = %d\n", tr->cost_server);
	printf("tr.cost_Parking = %d\n", tr->cost_Parking);

	for (i = 0; i < 7; i++)
		printf("tr.rateprice[%d] = %d\n", i, tr->rateprice[i]);

	printf("tr.carlogo = ");
	for (i = 0; i < 17; i++)
		printf("%02x", tr->carlogo[i]);
	printf("\n");

	if (data_nw.db_tran_type == TABLE_YCT) {
		printf("tr.yct.flag_card_settled=%d\n", tr->flag_card_settled);

		printf("tr.yct.TAC = ");
		for (i = 0; i < 4; i++)
			printf("%02x", tr->TAC[i]);
		printf("\n");

		printf("tr.yct.logic_cardNum = ");
		for (i = 0; i < 8; i++)
			printf("%02x", tr->yct_nosettle_trade.logic_cardNum[i]);
		printf("\n");

		printf("tr.yct.phy_cardNum = ");
		for (i = 0; i < 8; i++)
			printf("%02x", tr->yct_nosettle_trade.cardNum[i]);
		printf("\n");

		printf("tr.yct.ticket_trade_count=%d\n",
				tr->yct_nosettle_trade.ticket_trade_count);
		printf("tr.yct.trade_money=%d\n", tr->yct_nosettle_trade.trade_money);
		printf("tr.yct.balance=%d\n", tr->yct_nosettle_trade.balance);
	}
}

void debug_tr_xj(tran_record_xj* tr_xj) {
	int i = 0;
	printf("\n\n-------------------debug XJ  Tran Record-----------------\n");
	printf("trXJ.guntab = %d\n", tr_xj->guntab);
	printf("trXJ.flag_finished =%02x\n", tr_xj->flag_finished);

	printf("trXJ.serialnum = ");
	for (i = 0; i < 32; i++)
		printf("%c", tr_xj->serialnum[i]);
	printf("\n");
	//	printf("trXJ.serialnum = %s\n",tr_xj->serialnum);

	printf("trXJ.starttime = ");
	for (i = 0; i < 7; i++)
		printf("%02x", tr_xj->starttime[i]);
	printf("\n");

	printf("trXJ.endtime = ");
	for (i = 0; i < 7; i++)
		printf("%02x", tr_xj->endtime[i]);
	printf("\n");

	printf("trXJ.charge_duration = %d\n", tr_xj->charge_duration);

	printf("trXJ.start_soc = %d\n", tr_xj->start_soc);
	printf("trXJ.end_soc = %d\n", tr_xj->end_soc);

	printf("trXJ.userId = ");
	for (i = 0; i < 8; i++)
		printf("%02x", tr_xj->userId[i]);
	printf("\n");

	printf("trXJ.cardId = ");
	for (i = 0; i < 8; i++)
		printf("%02x", tr_xj->cardId[i]);
	printf("\n");

	printf("trXJ.stop_reason = %d\n", tr_xj->stop_reason);
	printf("trXJ.w_start = %d\n", tr_xj->w_start);
	printf("trXJ.w_end = %d\n", tr_xj->w_end);

	for (i = 0; i < 49; i++)
		printf("trXJ.w[%d] = %d\n", i, tr_xj->w[i]);

	printf("trXJ.cost_elec = %d\n", tr_xj->cost_elec);
	printf("trXJ.cost_server = %d\n", tr_xj->cost_server);

	printf("trXJ.carlogo = ");
	for (i = 0; i < 17; i++)
		printf("%02x", tr_xj->carlogo[i]);
	printf("\n");

	printf("trXJ.billmode = %d\n", tr_xj->billmode);
	printf("trXJ.chargevalue = %d\n", tr_xj->chargevalue);
	printf("trXJ.startmode = %d\n", tr_xj->startmode);
	printf("trXJ.g_stop_detail = %d\n", tr_xj->g_stop_detail);
}

void debug_err(err_record* err) {
	int i = 0;
	printf("----------------debug_err----------------\n");
	printf("err.guntab = %d\n", err->guntab);
	printf("err.time = ");
	for (i = 0; i < 7; i++)
		printf("%02x", err->time[i]);
	printf("\n");
	printf("err.type = %d\n", err->type);
}

#ifdef DEBUG
void debug_conf(sysset_cmd* sys)
{
	int i = 0;
	printf("sys.settag1 = %d\n",sys->settag1);
	printf("sys.bittag1 = %d\n",sys->bittag1);

	printf("sys.dev_num = ");
	for(i=0; i<8; i++)
		printf("%02x",sys->p1[i]);
	printf("\n");
	printf("sys.settag2 = %d\n",sys->settag2);
	printf("sys.bittag2 = %d\n",sys->bittag2);
	printf("sys.apn = %s\n",sys->p2);
	printf("sys.remote_ip1 = %d.%d.%d.%d\n",sys->p3[0],sys->p3[1],sys->p3[2],sys->p3[3]);
	printf("sys.port1 = %d\n",sys->p4);
	printf("sys.remote_ip2 = %d.%d.%d.%d\n",sys->p5[0],sys->p5[1],sys->p5[2],sys->p5[3]);
	printf("sys.port2 = %d\n",sys->p6);
	printf("sys.local_ip1 = %d.%d.%d.%d\n",sys->p7[0],sys->p7[1],sys->p7[2],sys->p7[3]);
	printf("sys.mask1 = %d.%d.%d.%d\n",sys->p8[0],sys->p8[1],sys->p8[2],sys->p8[3]);
	printf("sys.gateway1 = %d.%d.%d.%d\n",sys->p9[0],sys->p9[1],sys->p9[2],sys->p9[3]);
	printf("sys.local_ip2 = %d.%d.%d.%d\n",sys->p10[0],sys->p10[1],sys->p10[2],sys->p10[3]);
	printf("sys.mask2 = %d.%d.%d.%d\n",sys->p11[0],sys->p11[1],sys->p11[2],sys->p11[3]);
	printf("sys.gateway2 = %d.%d.%d.%d\n",sys->p12[0],sys->p12[1],sys->p12[2],sys->p12[3]);
	printf("sys.settag3 = %d\n",sys->settag3);
	printf("sys.rate_v = %.02f\n",sys->p13[0]);
	printf("sys.over_v = %.02f\n",sys->p13[1]);
	printf("sys.under_v = %.02f\n",sys->p13[2]);
	printf("sys.over_reagin_v = %.02f\n",sys->p13[3]);
	printf("sys.under_reagin_v = %.02f\n",sys->p13[4]);
	printf("sys.rate_a = %.02f\n",sys->p13[5]);
	printf("sys.over_a = %.02f\n",sys->p13[6]);
	printf("sys.under_a = %.02f\n",sys->p13[7]);
	printf("sys.over_reagin_a = %.02f\n",sys->p13[8]);
	printf("sys.under_reagin_a = %.02f\n",sys->p13[9]);
	printf("sys.con_time = %.02f\n",sys->p13[10]);

}

int debug_pm(charg_model* mode)
{
	int i =0;
	printf("mode.guntab = %d\n",mode->guntab);

	printf("mode.modelID = ");
	for(i=0; i<8; i++)
		printf("%02x",mode->modelID[i]);
	printf("\n");

	printf("mode.starttime = ");
	for(i=0; i<7; i++)
		printf("%02x",mode->starttime[i]);
	printf("\n");

	printf("mode.endtime = ");
	for(i=0; i<7; i++)
		printf("%02x",mode->endtime[i]);
	printf("\n");

	printf("mode.opstate = %d\n",mode->opstate);
	printf("mode.metertype = %d\n",mode->metertype);
	printf("mode.num = %d\n",mode->num);

	for(i=0; i<12; i++) {
		printf("mode.time[%d].time = %d\n",i,mode->time[i].time);
		printf("mode.time[%d].tag = %d\n",i,mode->time[i].tag);
	}

	for(i=0; i<7; i++)
		printf("mode.rateprice[%d] = %d\n",i,mode->rateprice[i]);
}

#endif

int debug_card(admin_card* card) {
	int i = 0;

	printf("card.card_id = ");
	for (i = 0; i < 8; i++)
		printf("%02x", card->card_id[i]);
	printf("\n");

	printf("card.card_type = %x\n", card->card_type);
	printf("card.card_order = %x\n", card->card_order);

	return 0;
}

void debug_gun_info(Gun_Info* gun)
{
	int i = 0;
	printf("gun.gunno=%d\n",gun->gunno);
	printf("gun.can_port=%d\n",gun->can_port);
	printf("gun.frame_gunno=%d\n",gun->frame_gunno);
	printf("gun.cha_addr=%d\n",gun->cha_addr);
	printf("gun.mt_port=%d\n",gun->mt_port);
	printf("gun.phase_num=%d\n",gun->phase_num);
	printf("gun.ct=%d\n",gun->ct);

	printf("gun.meteraddr=");
	for(i=0;i<6;i++)
		printf("%02x",gun->meteraddr[i]);
	printf("\n\n");
}

tran_record* init_tran_record(void) {
	tran_record* tr = NULL;

	do {
		tr = (tran_record*) malloc(sizeof(tran_record));
	} while (tr == NULL);
	memset(tr, 0, sizeof(tran_record));

	tr->guntab = 0;
	tr->flag_finished = TRAN_END;

	unsigned char serialnum[] = { 0x20, 0x18, 0x09, 0x15, 0x18, 0x49, 0x00,
			0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
	memcpy(tr->serialnum, &serialnum, sizeof(serialnum));

	unsigned char starttime[] = { 0x20, 0x17, 0x08, 0x02, 0x18, 0x4, 0x00 };
	memcpy(tr->starttime, &starttime, sizeof(starttime));

	unsigned char endtime[] = { 0x20, 0x17, 0x08, 0x07, 0x20, 0x16, 0x50 };
	memcpy(tr->endtime, &endtime, sizeof(endtime));

	tr->charge_duration = 20;
	unsigned char payCard[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
	memcpy(tr->payCard, &payCard, sizeof(payCard));

	unsigned char userId[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
	memcpy(tr->userId, &userId, sizeof(userId));

	unsigned char cardId[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
	memcpy(tr->cardId, &cardId, sizeof(cardId));

	unsigned char modelID[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
	memcpy(tr->modelID, &modelID, sizeof(modelID));

	tr->stop_reason = 0x01;
	tr->billbytm = 0;
	unsigned int w_start[] = { 0x40, 0x41, 0x42, 0x43, 0x44 };
	memcpy(tr->w_start, &w_start, sizeof(w_start));

	unsigned int w_end[] = { 0x50, 0x51, 0x52, 0x53, 0x54 };
	memcpy(tr->w_end, &w_end, sizeof(w_end));

	unsigned int w[] = { 0x60, 0x61, 0x62, 0x63, 0x64 };
	memcpy(tr->w, &w, sizeof(w));

	unsigned int cost_elec[] = { 0x71, 0x72, 0x73, 0x74, 0x75 };
	memcpy(tr->cost_elec, &cost_elec, sizeof(cost_elec));

	tr->cost_server = 12;
	tr->cost_Parking = 13;
	unsigned int rateprice[] = { 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77 };
	memcpy(tr->rateprice, &rateprice, sizeof(rateprice));

	unsigned char carlogo[] = { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xEA, 0xEB,
			0xEC, 0xED, 0XA0, 0XA1, 0xB1, 0x00, 0x2C, 0x2D, 0x01 };
	memcpy(tr->carlogo, &carlogo, sizeof(carlogo));

	if (data_nw.db_tran_type == TABLE_YCT) {
		tr->flag_card_settled = 0xaa;

		unsigned char tac[] = { 0x12, 0x34, 0x56, 0x78 };
		memcpy(tr->TAC, &tac, sizeof(tac));

		unsigned char logicCard[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xAA, 0x66,
				0x77 };
		memcpy(tr->yct_nosettle_trade.logic_cardNum, &logicCard,
				sizeof(logicCard));

		unsigned char phyCard[] = { 0xAB, 0xCD, 0xEF, 0x33, 0x44, 0xAA, 0x66,
				0xAA };
		memcpy(tr->yct_nosettle_trade.cardNum, &phyCard, sizeof(phyCard));
		tr->yct_nosettle_trade.ticket_trade_count = 10;
		tr->yct_nosettle_trade.trade_money = 55;
		tr->yct_nosettle_trade.balance = 12;
	}

	return tr;
}

tran_record_xj* init_tran_record_xj(void) {
	tran_record_xj* tr_xj = NULL;
	int i = 0, len = 0, tmp = 0;

	do {
		tr_xj = (tran_record_xj*) malloc(sizeof(tran_record_xj));
	} while (tr_xj == NULL);

	tr_xj->guntab = 1;
	tr_xj->flag_finished = TRAN_END;

	//	len=sizeof(tr_xj->serialnum)/sizeof(tr_xj->serialnum[0]);
	//	for(i=0; i<len; i++){
	//		tmp = inttoBCD(i+1);
	//		tr_xj->serialnum[i] = tmp;
	//	}
	char serialnum[32] = { 'T', 'S', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
			'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'A' };
	memcpy(tr_xj->serialnum, serialnum, 32);

	unsigned char starttime[] = { 0x20, 0x17, 0x08, 0x02, 0x18, 0x4, 0x00 };
	memcpy(tr_xj->starttime, &starttime, sizeof(starttime));

	unsigned char endtime[] = { 0x20, 0x17, 0x08, 0x07, 0x20, 0x16, 0x50 };
	memcpy(tr_xj->endtime, &endtime, sizeof(endtime));

	tr_xj->charge_duration = 20;
	tr_xj->start_soc = 44;
	tr_xj->end_soc = 88;

	unsigned char userId[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
	memcpy(tr_xj->userId, &userId, sizeof(userId));

	unsigned char cardId[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
	memcpy(tr_xj->cardId, &cardId, sizeof(cardId));

	tr_xj->stop_reason = 0x01;
	tr_xj->w_start = 123;
	tr_xj->w_end = 456;

	len = sizeof(tr_xj->w) / sizeof(tr_xj->w[0]);
	for (i = 0; i < len; i++)
		tr_xj->w[i] = i + 1;

	tr_xj->cost_elec = 22;

	tr_xj->cost_server = 46;

	unsigned char carlogo[] = { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xEA, 0xEB,
			0xEC, 0xED, 0XA0, 0XA1, 0xB1, 0x00, 0x2C, 0x2D, 0x01 };
	memcpy(tr_xj->carlogo, &carlogo, sizeof(carlogo));

	tr_xj->billmode = 47;
	tr_xj->chargevalue = 48;
	tr_xj->startmode = 49;
	tr_xj->g_stop_detail = 50;

	return tr_xj;
}

err_record* init_err_record(void) {
	err_record* err = NULL;

	do {
		err = (err_record*) malloc(sizeof(err_record));
	} while (err == NULL);

	err->guntab = 0;
	char time[] = { 0x20, 0x17, 0x08, 0x3, 0x15, 0x16, 0x9 };
	memcpy(err->time, &time, sizeof(time));
	err->type = 1;

	return err;
}

#if 0
sysset_cmd* init_sysset_cmd(void)
{
	sysset_cmd* cmd = NULL;

	do {
		cmd = (sysset_cmd*)malloc(sizeof(sysset_cmd));
	}while(cmd == NULL);

	cmd->settag1 = 1;
	cmd->bittag1 = 2;
	cmd->p1 = {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17};
	cmd->settag2 = 3;
	cmd->bittag2 = 4;
	cmd->p2 = "3G_NET";
	cmd->p3 = {192,168,1,230};
	cmd->p4 = 2001;
	cmd->p5 = {192,168,1,232};
	cmd->p6 = 2002;
	cmd->p7 = {192,168,2,222};
	cmd->p8 = {192,168,3,202};
	cmd->p9 = {225,225,225,0};
	cmd->p10 = {10,111,186,1};
	cmd->p11 = {98,32,34,21};
	cmd->p12 = {1,1,1,1};
	cmd->settag3 = 5;
	cmd->p13 = {350.01,380,320.5,401,200,120,90.5,110,6,232.22,88.88};

	return cmd;
}

charg_model* init_charge_mode(void)
{
	charg_model* model = NULL;

	do {
		model = (charg_model*)malloc(sizeof(charg_model));
	}while(model == NULL);

	model->guntab = 0;
	model->modelID = {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17};
	model->starttime = {0x20,0x17,0x08,0x11,0x18,0x10,0x00};
	model->endtime = {0x20,0x25,0x01,0x1,0x18,0x10,0x00};
	model->opstate = 1;
	model->metertype = 2;
	model->num = 3;
	model->time[0] = {0,1};
	model->time[1] = {2,2};
	model->time[2] = {4,3};
	model->time[3] = {6,4};
	model->time[4] = {8,1};
	model->time[5] = {10,2};
	model->time[6] = {12,3};
	model->time[7] = {14,4};
	model->time[8] = {16,1};
	model->time[9] = {18,2};
	model->time[10] = {20,3};
	model->time[11] = {22,4};
	model->rateprice[0] = 12;
	model->rateprice[1] = 13;
	model->rateprice[2] = 14;
	model->rateprice[3] = 15;
	model->rateprice[4] = 16;
	model->rateprice[5] = 17;
	model->rateprice[6] = 18;

	return model;
}

admin_card card[] = {
		{
				.card_id = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88},
				.card_type = 0x0A,
				.card_order = 1,
		},
		{
				.card_id = {0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x12},
				.card_type = 0x0A,
				.card_order = 2,
		},
		{
				.card_id = {0x11,0x33,0x55,0x77,0x00,0x00,0x00,0x99},
				.card_type = 0x0B,
				.card_order = 1,
		},
		{
				.card_id = {0x22,0x44,0x66,0x88,0x00,0x00,0x00,0x00},
				.card_type = 0x0B,
				.card_order = 2,
		},
		{
				.card_id = {0x98,0x76,0x54,0x32,0x10,0x00,0x00,0x11},
				.card_type = 0x0B,
				.card_order = 3,
		},
};

#endif

unsigned int inttoBCD(int m) {
	unsigned int r = 0, n = 1;
	int a, i = 0;
	while (m) {
		a = m % 10;
		m = m / 10;
		a = a << (4 * i);
		i++;
		r |= a;
	}
	return r;
}

void debug_xj_pm(xj_price_model* model){
	int i=0;
	float value=0.0;

	printf("###### XJ  PriceModel ###########");
	for(i=0; i<48; i++){
		value = ((float)(model->power_rate[i])/10000);
		printf("power_rate[%d]=%.04f\n",i,value);
	}
	printf("\n");
	for(i=0; i<48; i++){
		value = ((float)(model->service_rate[i])/10000);
		printf("service_rate[%d]=%.04f\n",i,value);
	}
	printf("######### END ##############");
}

int main(int argc, char* argv[]) {
	int i,j;
	tran_record *query_tr = NULL;
	err_record *query_err = NULL;
	tran_record_xj* query_tr_xj = NULL;

#ifdef DEBUG
	sysset_cmd* query_sys = NULL;
	charg_model* query_mode = NULL;
#endif

	admin_card* query_card = NULL;
	clock_t start, end;
	int ret = 0;

	printf("Start=================================\n\n");
	start = clock();

	do {
		query_tr = (tran_record*) malloc(sizeof(tran_record) * TR_QUERY_NUM);
	} while (query_tr == NULL);

	do {
		query_tr_xj = (tran_record_xj*) malloc(
				sizeof(tran_record_xj) * TR_QUERY_NUM);
	} while (query_tr_xj == NULL);

	do {
		query_err = (err_record*) malloc(sizeof(err_record) * ERR_QUERY_NUM);
	} while (query_err == NULL);


#ifdef DEBUG
	do {
		query_sys = (sysset_cmd*)malloc(sizeof(sysset_cmd));
	}while(query_sys == NULL);

	do {
		query_mode = (charg_model*)malloc(sizeof(charg_model));
	}while(query_sys == NULL);
#endif

	do {
		query_card = (admin_card*) malloc(sizeof(admin_card) * 5);
	} while (query_card == NULL);

	memset(query_tr, 0, sizeof(tran_record) * TR_QUERY_NUM);
	memset(query_err, 0, sizeof(err_record) * ERR_QUERY_NUM);

#ifdef DEBUG
	memset(query_sys,0,sizeof(sysset_cmd));
	memset(query_mode,0,sizeof(charg_model));
#endif

	memset(query_card, 0, sizeof(admin_card));

#if	0 //南网交易记录测试
	protocol_database_register(&data_nw);
#if 1
	tran_record* update_tr_nw = NULL;

	for(i=0; i<15; i++)
	{
		update_tr_nw = init_tran_record();
		if(i>=0 && i<255)
			update_tr_nw->serialnum[15] = inttoBCD(i);
		if( i%3 == 0)
			update_tr_nw->flag_finished = TRAN_OFFLINE;
		//		else if(i%5 == 0)
		//			update_tr->flag_finished = TRAN_START;

		printf("protocol_updateCD...\n");
		protocol_updateCD(update_tr_nw);
	}
#endif

#if  1
	printf("protocol_queryCD...\n");
	ret = protocol_queryCD(query_tr,TR_QUERY_NUM);
	for(i=0; i<ret; i++)
	{
		debug_tr(&query_tr[i]);
	}
	free(query_tr);
	printf("----------------------------------------\n\n\n");
#endif

#if 1
	tran_record offline_tr_cd_nw;
	printf("protocol_get_offlineCD...\n");
	ret = protocol_get_offlineCD(&offline_tr_cd_nw);
	if(ret == 0) {
		debug_tr(&offline_tr_cd_nw);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	}
	else {
		printf("protocol_get_offlineCD Error----------------------\n");
	}
#endif

#if 1
	tran_record tr_ser_nw;
	printf("protocol_getCD_bySerialnum...\n");
	memcpy(tr_ser_nw.serialnum,update_tr_nw->serialnum,sizeof(tr_ser_nw.serialnum));
	ret = protocol_getCD_bySerialnum(&tr_ser_nw);
	if(ret == 0) {
		debug_tr(&tr_ser_nw);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	}
	else {
		printf("protocol_getCD_bySerialnum Error----------------------\n");
	}
#endif

#if 1
	tran_record tr_serIndex_nw;
	printf("protocol_get_offlineCD_byIndex...\n");

	ret = protocol_get_offlineCD_byIndex(&tr_serIndex_nw,1);
	if(ret == 0) {
		debug_tr(&tr_serIndex_nw);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	}
	else {
		printf("protocol_get_offlineCD_byIndex Error----------------------\n");
	}
#endif

#if 1
	tran_record offline_tr_nw;
	printf("------get offline tran record!!!\n");
	i=1;
	while(1) {
		memset(&offline_tr_nw,0,sizeof(offline_tr_nw));
		printf("----------i=%d\n",i);
		ret = protocol_get_offlineCD(&offline_tr_nw);
		if(ret == 0) {
			debug_tr(&offline_tr_nw);
			offline_tr_nw.flag_finished = TRAN_END;
			protocol_updateCD(&offline_tr_nw);
			i++;
			sleep(2);
			continue;
		}
		else {
			printf("exit get offline tr...\n");
			break;
		}
	}
#endif

#endif

#if	0//南网羊城充扣费
	data_nw.db_tran_type=TABLE_YCT;
	protocol_database_register(&data_nw);
#if 1
	tran_record* update_tr = NULL;

	for(i=0; i<15; i++)
	{
		update_tr = init_tran_record();
		if(i>=0 && i<255)
			update_tr->serialnum[15] = inttoBCD(i);
		if( i%3 == 0)
			update_tr->flag_finished = TRAN_OFFLINE;
		else if(i%5 == 0)
			update_tr->flag_card_settled = 0x55;

		printf("protocol_updateCD...\n");
		protocol_updateCD(update_tr);
	}
#endif

#if  1
	printf("protocol_queryCD...\n");
	ret = protocol_queryCD(query_tr,TR_QUERY_NUM);
	for(i=0; i<ret; i++)
	{
		debug_tr(&query_tr[i]);
	}
	free(query_tr);
	printf("----------------------------------------\n\n\n");
#endif

#if 1
	tran_record offline_tr_cd;
	printf("protocol_get_offlineCD...\n");
	ret = protocol_get_offlineCD(&offline_tr_cd);
	if(ret == 0) {
		debug_tr(&offline_tr_cd);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	}
	else {
		printf("protocol_get_offlineCD Error----------------------\n");
	}
#endif

#if 1
	tran_record tr_ser;
	printf("protocol_getCD_bySerialnum...\n");
	memcpy(tr_ser.serialnum,update_tr->serialnum,sizeof(tr_ser.serialnum));
	ret = protocol_getCD_bySerialnum(&tr_ser);
	if(ret == 0) {
		debug_tr(&tr_ser);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	}
	else {
		printf("protocol_getCD_bySerialnum Error----------------------\n");
	}
#endif

#if 1
	tran_record tr_serIndex;
	printf("protocol_get_offlineCD_byIndex...\n");

	ret = protocol_get_offlineCD_byIndex(&tr_serIndex,1);
	if(ret == 0) {
		debug_tr(&tr_serIndex);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	}
	else {
		printf("protocol_get_offlineCD_byIndex Error----------------------\n");
	}
#endif

#if 1
	tran_record offline_tr;
	printf("------get offline tran record!!!\n");
	i=1;
	while(1) {
		memset(&offline_tr,0,sizeof(offline_tr));
		printf("----------i=%d\n",i);
		ret = protocol_get_offlineCD(&offline_tr);
		if(ret == 0) {
			debug_tr(&offline_tr);
			offline_tr.flag_finished = TRAN_END;
			protocol_updateCD(&offline_tr);
			i++;
			sleep(1);
			continue;
		}
		else {
			printf("exit get offline tr...\n");
			break;
		}
	}
#endif
	tran_record card_tr;
	printf("------get incomplete record!!!\n");
	i=1;
	while(1) {
		memset(&card_tr,0,sizeof(card_tr));
		printf("----------i=%d\n",i);
		ret = protocol_get_offlineCD_byCardnum(update_tr->cardId,&card_tr);
		if(ret == 0) {
			debug_tr(&card_tr);
			card_tr.flag_card_settled = 0xaa;
			protocol_updateCD(&card_tr);
			i++;
			sleep(1);
			continue;
		}
		else {
			printf("exit get offline tr...\n");
			break;
		}
	}
#if 1

#endif

#endif

#if 1//小橘测试
	protocol_database_register(&data_xj);

	xj_price_model model;
	memset(&model,0,sizeof(model));

#if 1
	if(protocol_get_PriceModel(&model) == ERROR_OK)
		debug_xj_pm(&model);
#endif

#if 0
	memset(&model,0,sizeof(model));
	for(i=0; i<48; i++){
		model.power_rate[i]=33333;
		model.service_rate[i]=55555;
	}

	if(protocol_update_PriceModel(&model) == ERROR_OK){
		if(protocol_get_PriceModel(&model) == ERROR_OK)
				debug_xj_pm(&model);
	}
#endif

#if 0
	tran_record_xj* update_tr_xj = NULL;

	for (i = 0; i < 15; i++) {
		update_tr_xj = init_tran_record_xj();
		if (i >= 0 && i < 255)
			update_tr_xj->serialnum[31] += i;

		if (i % 3 == 0)
			update_tr_xj->flag_finished = TRAN_OFFLINE;
		//		else if(i%5 == 0)
		//			update_tr->flag_finished = TRAN_START;

		printf("protocol_updateCD...\n");
		protocol_updateCD(update_tr_xj);
	}
#endif

#if  0
	printf("protocol_queryCD...\n");
	ret = protocol_queryCD(query_tr_xj, TR_QUERY_NUM);
	for (i = 0; i < ret; i++) {
		debug_tr_xj(&query_tr_xj[i]);
	}
	free(query_tr_xj);
	printf("----------------------------------------\n\n\n");
#endif

#if 0
	tran_record_xj offlineXJ_tr_cd;
	printf("protocol_get_offlineCD...\n");
	ret = protocol_get_offlineCD(&offlineXJ_tr_cd);
	if (ret == 0) {
		debug_tr_xj(&offlineXJ_tr_cd);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	} else {
		printf("protocol_get_offlineCD Error----------------------\n");
	}
#endif

#if 0
	tran_record_xj trXJ_ser;
	printf("protocol_getCD_bySerialnum...\n");
	memcpy(trXJ_ser.serialnum, update_tr_xj->serialnum,
			sizeof(trXJ_ser.serialnum));
	ret = protocol_getCD_bySerialnum(&trXJ_ser);
	if (ret == 0) {
		debug_tr_xj(&trXJ_ser);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	} else {
		printf("protocol_getCD_bySerialnum Error----------------------\n");
	}
#endif

#if 0
	tran_record_xj trXJ_serIndex;
	printf("protocol_get_offlineCD_byIndex...\n");

	ret = protocol_get_offlineCD_byIndex(&trXJ_serIndex, 2);
	if (ret == 0) {
		debug_tr_xj(&trXJ_serIndex);
		//			offline_tr.flag_finished = TRAN_END;
		//			protocol_updateCD(&offline_tr);
	} else {
		printf("protocol_get_offlineCD_byIndex Error----------------------\n");
	}
#endif

#if 0
	tran_record_xj offline_trXJ;
	printf("------get offline tran record!!!\n");
	i = 1;
	while (1) {
		memset(&offline_trXJ, 0, sizeof(offline_trXJ));
		printf("----------i=%d\n", i);
		ret = protocol_get_offlineCD(&offline_trXJ);
		if (ret == 0) {
			debug_tr_xj(&offline_trXJ);
			offline_trXJ.flag_finished = TRAN_END;
			protocol_updateCD(&offline_trXJ);
			i++;
			sleep(2);
			continue;
		} else {
			printf("exit get offline tr...\n");
			break;
		}
	}
#endif

#endif

	//====================================================
#if 0
	err_record* err = init_err_record();
	for(i=0; i<15; i++) {
		err->type = i;
		err->time[6] = inttoBCD(i);
		protocol_updateERR(err);
	}

	protocol_queryERR(query_err,ERR_QUERY_NUM);
	for(i=0; i<ERR_QUERY_NUM; i++)
		debug_err(&query_err[i]);
#endif

#if 0
	for(i=0; i<3; i++) {
		sys.p13[10] += i;
		protocol_updateSYSCONF(&sys);
	}
	protocol_querySYSCONF(query_sys);
	debug_conf(query_sys);
#endif

#if 0
	for(i=0; i<3; i++) {
		protocol_updatePM(&mode);
	}
	protocol_queryPM(query_mode);
	debug_pm(query_mode);
#endif

#if 0
	for(i=0; i<5; i++)
		protocol_updateCard(&card[i], 1);

	protocol_queryCard(query_card);
	for(i=0; i<5; i++)
		debug_card(&query_card[i]);
#endif

#if 0
	blacklist_card black= {
			.cardNum= {0x55,0x34,0x00,0x00,0x00,0x00,0x00,0x01},
			.state =1,
	};

	for(i=0; i<20; i++) {
		black.cardNum[7] = i+1;
		if(protocol_update_blacklistCard(&black) == 0) {
			printf("insert ok\n");
		}
		else {
			printf("insert fail!");
		}
	}

	for(i=0; i<20; i++) {
		black.cardNum[7] = i*2;
		if(protocol_blacklistCard_isExist(black.cardNum) == 1) {
			printf("query ok, exist!\n");
		}
		else
			printf("query ok,no  exist!\n");
	}

	for(i=0; i<20; i++) {
		black.cardNum[7] = i+1;
		black.state=2;
		if(protocol_update_blacklistCard(&black) == 0) {
			printf("delete ok\n");
		}
		else {
			printf("delete fail!");
		}
	}

#endif

#if 0 //充电枪测试
	int count = 0;
	Gun_Info gun;

	memset(&gun,0,sizeof(gun));
	count = atoi(argv[1]);
	if(count==0)
		count=1;

	for(i=0; i<count; i++){
		for(j=0;j<8;j++){
			protocol_get_GunInfo(j,&gun);
			debug_gun_info(&gun);
		}
		printf("**************************\n");
	}
#endif

	end = clock();

	printf("End=================================\n\n");
	printf("This Program Run Time:%lf \n",
			(double) (end - start) / CLOCKS_PER_SEC);

	return 0;
}
